package com.xsz.pda.ui

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.Spinner
import android.widget.Switch
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.ToastUtils
import com.xsz.pda.App
import com.xsz.pda.AppExecutors
import com.xsz.pda.Config
import com.xsz.pda.R
import com.xsz.pda.api.TicketServiceApi
import com.xsz.pda.api.beans.DeviceGroup
import com.xsz.pda.data.model.TicketModel
import com.xsz.pda.device.eid.ReadCardManager
import com.xsz.pda.net.api.Api


class SettingsActivity : AppCompatActivity() {


    private lateinit var edDeviceName: EditText
//    private lateinit var edProjectNo: EditText
    private lateinit var edGroupId: Spinner
    private lateinit var openFirstCheck: Switch
    private lateinit var openCheckIdCard: Switch


    private val api = Api(Config.ip)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_settings)

//        edDeviceName = findViewById(R.id.edDeviceName)
        openFirstCheck = findViewById(R.id.firstCheckSw)
        openCheckIdCard = findViewById(R.id.checkIdCard)
//        edProjectNo = findViewById(R.id.edProjectNo)
        edGroupId = findViewById(R.id.edGroupId)
//        edDeviceName.setText(DeviceUtils.getAndroidID())

        AppExecutors.networkIO.execute {
            TicketServiceApi(Config.ip).getAllGroups({
                if (it.isNotEmpty()){
                    runOnUiThread {
                        val adapter: ArrayAdapter<DeviceGroup> =
                            ArrayAdapter<DeviceGroup>(applicationContext, android.R.layout.simple_spinner_dropdown_item, it)
                        edGroupId.adapter = adapter
                        val group = Config.getGroup()
                        if (!TextUtils.isEmpty(group)) {
                            val dg = DeviceGroup()
                            dg.id = group
                            val idx=it.indexOf(dg)
                            if (idx>-1){
                                edGroupId.setSelection(idx)
                            }
                        }
                    }
                }

            }){
                ToastUtils.showShort(it)
            }
        }

        val deviceName = Config.getName()
        val projectNo = Config.getProjectNo()

//        edDeviceName.setText(deviceName)
        openFirstCheck.isChecked = Config.isOpenFirstCheck()
        openCheckIdCard.isChecked = Config.isCheckIDCARD()
//        edProjectNo.setText(projectNo)

        findViewById<Button>(R.id.saveSettings).setOnClickListener {

//            var deviceNameNew = edDeviceName.text.toString()
            Config.saveOpenFirstCheck(openFirstCheck.isChecked)
            Config.saveCheckIDCARD(openCheckIdCard.isChecked)
            if (openCheckIdCard.isChecked) { ReadCardManager.initEid(this) }
            App.openFirstCheck = openFirstCheck.isChecked
            App.checkIdCard = openCheckIdCard.isChecked
//            val projectNoNew = edProjectNo.text.toString()
            //必须填写需要的信息
//            if (StringUtils.isEmpty(deviceNameNew)) {
////                ToastUtils.showShort("设备名称不能为空！")
//                deviceNameNew = DeviceUtils.getAndroidID()
//            }
            val groupId = (edGroupId.selectedItem as DeviceGroup).id
            api.register(
                DeviceUtils.getAndroidID(),
                DeviceUtils.getAndroidID(),
                groupId, { projectId, projectNo, ticketTypes ->
                    Config.saveProjectId(projectId ?: "")
                    Config.saveProjectNo(projectNo ?: "")
                    Config.setGroupId(groupId)
                    App.groupId = groupId
                    ticketTypes?.let { Config.setTicketTypeIds(it.toSet()) }
                    Config.saveName(DeviceUtils.getAndroidID())
                    TicketModel.clearProjectData()
                    runOnUiThread{ onBackPressed()}
            },{
                ToastUtils.showShort(it)
                return@register
            })
        }

        findViewById<Button>(R.id.cancelSettings).setOnClickListener { onBackPressed() }
    }

//    override fun onBackPressed() {
//        super.onBackPressed()
//        val deviceName = Config.getName()
//        val projectNo = Config.getProjectNo()
//        if (!StringUtils.isEmpty(deviceName) && !StringUtils.isEmpty(projectNo)) {
//            startActivity(Intent(this, MainActivity::class.java))
//            finish()
//        }else{
//            ToastUtils.showShort("请设置设备号和项目号！")
//        }
//    }
}